

//
//  LeeErWeiMaViewController.m
//  firstpoint
//
//  Created by 李雪虎 on 2017/7/25.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "LeeErWeiMaViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface LeeErWeiMaViewController ()<AVCaptureMetadataOutputObjectsDelegate>
{
    AVCaptureSession *session;
    AVCaptureVideoPreviewLayer *layer;
}
@end

@implementation LeeErWeiMaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"二维码扫描";
   self.navigationController.navigationBar.hidden=NO;
    [self btnClick];
    // Do any additional setup after loading the view.
}
-(void)btnClick{                
    AVCaptureDevice *device= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    session = [[AVCaptureSession alloc]init];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    NSError *error = nil;
    if (input) {
        [session addInput:input];
    }else{
        NSLog(@"error----%@",[error localizedDescription]);
    }
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session setSessionPreset:AVCaptureSessionPresetHigh];
    [session addOutput:output];
    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code];
    layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    layer.frame = CGRectMake(50, 170, 280, 280);
    [self.view.layer insertSublayer:layer atIndex:0];
    [session startRunning];   
}
#pragma mark -- AVCaptureMetadataOutputObjectsDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    NSString *zhQRCode = nil;
    for (AVMetadataObject *metadata in metadataObjects) {
        if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
            zhQRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
            break;
        }
    }
    [session stopRunning];//结束
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
